package com.baosight.sczx.common.zm.basetools.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.cf.MessageUtil;

public class GenerateAlarmPointMessage 
{
	private Dao dao;
	
	public GenerateAlarmPointMessage(){
	}
	
	public void setDao(Dao dao)
	{
		this.dao = dao;
	}
	
	//发送预警消息
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int sendAlarmMessage()
	{
		try
		{
			//提醒进度类型的预警
			List lengthTypeList = queryAlarmInfo(0);
			List keyIdList = new ArrayList();
			
			for(int i=0; i<lengthTypeList.size(); i++)
			{
				Map resultMap = (Map)lengthTypeList.get(i);
				
				String message = "空提醒消息";
				if(resultMap.get("fdInformation") != null)
				{
					message = (String)resultMap.get("fdInformation");
				}
				sendMessage(message);
				
				//记录预警设置的主键，用于更新最后预警时间
				keyIdList.add(resultMap.get("fdKeyId"));
			}
			
			//提醒日期类型的预警
			List dateTypeList = queryAlarmInfo(1);
			
			for(int i=0; i<dateTypeList.size(); i++)
			{
				Map resultMap = (Map)dateTypeList.get(i);
				
				String message = "【预警消息】";
				if(resultMap.get("fdInformation") != null)
				{
					message += (String)resultMap.get("fdInformation");
				}
				sendMessage(message);
				
				//记录预警设置的主键，用于更新最后预警时间
				keyIdList.add(resultMap.get("fdKeyId"));
			}
			
			//更新预警设置的最后预警时间
			updateLastDate(keyIdList);
		}
		catch (Exception e) 
		{
			System.out.println("发送预警消息异常 : " + e.getMessage());
			return 0;
		}
		
		return 1;
	}
	
	/**
	 * 
	 * <p>Function: queryAlarmInfo</p>
	 * <p>Description: 查询预警信息</p>
	 * <p>传入参数alarmType: 0:进度;1:日期</p>
	 * <p>查询不到值，然后空List</p>
	 * <p>return value:List</p>
	 * <p>History:
	 *    2011-8-29下午06:03:45 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List queryAlarmInfo(int alarmType)
	{
		List resultList = new ArrayList();
		if(alarmType != 0 && alarmType != 1)
		{
			return new ArrayList();
		}
		
		Map _map = new HashMap();
		_map.put("fdSwitch", "开");
		
		if(0 == alarmType)
		{
			_map.put("fdAlarmType", "进度");
			_map.put("fdStartData", StaticTools.getDate());
			_map.put("fdEndData", StaticTools.getDate());
			_map.put("fdLastDate", StaticTools.getDate());
			resultList = dao.query("ZMCPAlarmPoint.queryWithProject", _map);
		}
		
		if(1 == alarmType)
		{
			_map.put("fdAlarmType", "日期");
			_map.put("fdStartData", StaticTools.getDate());
			_map.put("fdEndData", StaticTools.getDate());
			_map.put("fdLastDate", StaticTools.getDate());
			resultList = dao.query("ZMCPAlarmPoint.queryWithCondition", _map);
		}
		
		return resultList;
	}
	
	/**
	 * 
	 * <p>Function: sendMessage</p>
	 * <p>Description: 给所有设置的消息接收人发送消息</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2011-8-29下午06:27:45 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private void sendMessage(String message)
	{
		MessageUtil util = new MessageUtil();
		List userList = dao.query("ZMCPAlarmPeopleSetting.queryUnique", new HashMap());
		if(userList.size() > 0)
		{
			util.setDao(dao);
		}
		
		for(int i=0; i<userList.size(); i++)
		{
			String userId = (String)((Map)userList.get(i)).get("fdPeopleId");
			int returnValue = util.insertIntoMessageCache(message, "ZMCF05", "", userId, 4);
			
			if(returnValue != 1)
			{
				return;
			}
		}
	}
	
	/**
	 * 
	 * <p>Function: updateLastDate</p>
	 * <p>Description: 更新消息最后发送日期</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2011-8-29下午06:17:28 bobswang
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateLastDate(List keyIds)
	{
		String currentDate = StaticTools.getDate();
		for(int i=0; i<keyIds.size(); i++)
		{
			Map _map = new HashMap();
			_map.put("fdKeyId", keyIds.get(i));
			_map.put("fdLastDate", currentDate);
			
			dao.update("ZMCPAlarmPoint.updateDate", _map);
		}
	}
}
